/**
 * This example shows how to connect to Twitter, display authorization dialog and save user's token
 * and username.
 */

package net.widevision.android;

import net.widevision.android.TwitterApp.TwDialogListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class TestConnect extends Activity {
	private TwitterApp mTwitter;
//	private CheckBox mTwitterBtn;

	private static final String twitter_consumer_key = "JXmXcE5yFPLIRCCtQ79eg";
	private static final String twitter_secret_key = "dnVojAwaewvEt8E2QwLYtavwm8cdUHwph7lZCaQPMY";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

//		mTwitterBtn.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				onTwitterClick();
//			}
//		});
		
		mTwitter 	= new TwitterApp(this, twitter_consumer_key,twitter_secret_key);
		
		mTwitter.setListener(mTwLoginDialogListener);
		
//		if (mTwitter.hasAccessToken()) {
//			mTwitterBtn.setChecked(true);
//			
//			String username = mTwitter.getUsername();
//			username		= (username.equals("")) ? "Unknown" : username;
//			
//			mTwitterBtn.setText("  Twitter (" + username + ")");
//			mTwitterBtn.setTextColor(Color.WHITE);
//		}
		
		Button goBtn = (Button) findViewById(R.id.button1);
		
		goBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(TestConnect.this, TestPost.class));
			}
		});
	}
	
//	private void onTwitterClick() {
//		if (mTwitter.hasAccessToken()) {
//			final AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			
//			builder.setMessage("Delete current Twitter connection?")
//			       .setCancelable(false)
//			       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
//			           public void onClick(DialogInterface dialog, int id) {
//			        	   mTwitter.resetAccessToken();
//			        	   
//			        	   mTwitterBtn.setChecked(false);
//			        	   mTwitterBtn.setText("  Twitter (Not connected)");
//			        	   mTwitterBtn.setTextColor(Color.GRAY);
//			           }
//			       })
//			       .setNegativeButton("No", new DialogInterface.OnClickListener() {
//			           public void onClick(DialogInterface dialog, int id) {
//			                dialog.cancel();
//			                
//			                mTwitterBtn.setChecked(true);
//			           }
//			       });
//			final AlertDialog alert = builder.create();
//			
//			alert.show();
//		} else {
//			mTwitterBtn.setChecked(false);
//			
//			mTwitter.authorize();
//		}
//	}

	private final TwDialogListener mTwLoginDialogListener = new TwDialogListener() {
		@Override
		public void onComplete(String value) {
			String username = mTwitter.getUsername();
			username		= (username.equals("")) ? "No Name" : username;
		
//			mTwitterBtn.setText("  Twitter  (" + username + ")");
//			mTwitterBtn.setChecked(true);
//			mTwitterBtn.setTextColor(Color.WHITE);
			
			Toast.makeText(TestConnect.this, "Connected to Twitter as " + username, Toast.LENGTH_LONG).show();
		}
		
		@Override
		public void onError(String value) {
//			mTwitterBtn.setChecked(false);
			
			Toast.makeText(TestConnect.this, "Twitter connection failed", Toast.LENGTH_LONG).show();
		}
	};
}